package com.caipin.controller;

/**
 * @Author: zhunian0322@163.com
 * @DateTime: 2021-09-30 9:23
 * @Description: TODO
 */
@RestController
@RequestMapping({"/dataExport"})
public class DataExportController {
    @Autowired
    private JZHGateManagementService jzhGateManagementService;

    @Autowired
    private CameraPlatformManagementService cameraPlatformManagementService;

    @GetMapping({"/bodyTemperatureSummary"})
    public void getBodyTemperatureSummary(@RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, HttpServletResponse response) {
        String filename = this.jzhGateManagementService.createExcelFile(startTime, endTime);
        File file = new File(filename);
        if (file.exists()) {
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment;fileName=" + filename);
            byte[] buff = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                ServletOutputStream outputStream = response.getOutputStream();
                int size = bis.read(buff);
                while (-1 != size) {
                    outputStream.write(buff, 0, size);
                    size = bis.read(buff);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fis != null)
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                if (bis != null)
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                try {
                    FileUtils.forceDelete(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @GetMapping({"/classroomBreakInSummary"})
    public void getClassroomBreakInSummary(@RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, HttpServletRequest request, HttpServletResponse response) {
        String filename = this.cameraPlatformManagementService.createExcelFileInvasionReport(startTime, endTime, request.getLocalAddr());
        File file = new File(filename);
        if (file.exists()) {
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment;fileName=" + filename);
            byte[] buff = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                ServletOutputStream outputStream = response.getOutputStream();
                int size = bis.read(buff);
                while (-1 != size) {
                    outputStream.write(buff, 0, size);
                    size = bis.read(buff);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fis != null)
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                if (bis != null)
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                try {
                    FileUtils.forceDelete(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @GetMapping({"/classroomBreakInList"})
    public Object getClassroomBreakInList(@RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize, @RequestParam("clientIp") String clientIp) {
        PageBean<ClassInvasionSummaryExcel> pageBean = this.cameraPlatformManagementService.findClassroomBreakInList(startTime, endTime, pageNum, pageSize, clientIp);
        return ResultJson.ok(pageBean);
    }

    @GetMapping({"/strangerWarningSummary"})
    public void getStrangerWarningSummary(@RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, @RequestParam("clientIp") String clientIp, HttpServletResponse response) {
        String filename = this.cameraPlatformManagementService.createExcelFileStrangerWarning(startTime, endTime, clientIp);
        File file = new File(filename);
        if (file.exists()) {
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment;fileName=" + filename);
            byte[] buff = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                ServletOutputStream outputStream = response.getOutputStream();
                int size = bis.read(buff);
                while (-1 != size) {
                    outputStream.write(buff, 0, size);
                    size = bis.read(buff);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fis != null)
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                if (bis != null)
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                try {
                    FileUtils.forceDelete(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @GetMapping({"/strangerWarningList"})
    public Object getStrangerWarningList(@RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize, @RequestParam("clientIp") String clientIp) {
        PageBean<StrangerWarningExcel> pageBean = this.cameraPlatformManagementService.findStrangerWarningList(startTime, endTime, pageNum, pageSize, clientIp);
        return ResultJson.ok(pageBean);
    }

    @GetMapping({"/crowdWarningSummary"})
    public void getCrowdWarningSummary(@RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, @RequestParam("clientIp") String clientIp, HttpServletResponse response) {
        String filename = this.cameraPlatformManagementService.createExcelFileCrowdWarning(startTime, endTime, clientIp);
        File file = new File(filename);
        if (file.exists()) {
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment;fileName=" + filename);
            byte[] buff = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                ServletOutputStream outputStream = response.getOutputStream();
                int size = bis.read(buff);
                while (-1 != size) {
                    outputStream.write(buff, 0, size);
                    size = bis.read(buff);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fis != null)
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                if (bis != null)
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                try {
                    FileUtils.forceDelete(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @GetMapping({"/crowdWarningList"})
    public Object getCrowdWarningList(@RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize, @RequestParam("clientIp") String clientIp) {
        PageBean<CrowdWarningExcel> pageBean = this.cameraPlatformManagementService.findCrowdWarningList(startTime, endTime, pageNum, pageSize, clientIp);
        return ResultJson.ok(pageBean);
    }

    @GetMapping({"/courseStatusSummary"})
    public void getCourseStatusSummary(@RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, HttpServletResponse response) {
        String filename = this.cameraPlatformManagementService.createExcelFileCourseStatusSummaryByCourse(startTime, endTime);
        File file = new File(filename);
        if (file.exists()) {
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment;fileName=" + filename);
            byte[] buff = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                ServletOutputStream outputStream = response.getOutputStream();
                int size = bis.read(buff);
                while (-1 != size) {
                    outputStream.write(buff, 0, size);
                    size = bis.read(buff);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fis != null)
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                if (bis != null)
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                try {
                    FileUtils.forceDelete(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @GetMapping({"/classStatusSummary"})
    public void getClassStatusSummary(@RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, HttpServletResponse response) {
        String filename = this.cameraPlatformManagementService.createExcelFileCourseStatusSummaryByClassroom(startTime, endTime);
        File file = new File(filename);
        if (file.exists()) {
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment;fileName=" + filename);
            byte[] buff = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                ServletOutputStream outputStream = response.getOutputStream();
                int size = bis.read(buff);
                while (-1 != size) {
                    outputStream.write(buff, 0, size);
                    size = bis.read(buff);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fis != null)
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                if (bis != null)
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                try {
                    FileUtils.forceDelete(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

